<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="qq" fill="pink" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog

      :title="isEdit? '编辑':'添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <AddorEdit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @close="hClose" @success="success" />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptDialog.vue'
export default {
  name: 'Departments',
  components: { AddorEdit },
  data() {
    return {
      showDialog: false,
      // 依赖一份树形数据
      list: [],
      originList: [],
      curId: null,
      isEdit: false

    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 获取组织架构信息
    async loadDepartments() {
      try {
        const res = await getDepartments()
        res.data.depts.shift()
        this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log(e)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    success() {
      this.showDialog = false
      this.loadDepartments()
    },
    hDel(id) {
      this.$confirm('真的要删除吗?嘤嘤嘤!', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delDepartment(id)
          this.$message.success(res.message)
          this.loadDepartments()
        } catch (error) {
          this.$message.success(error.message)
        }
      })
    },
    hClose() {
      this.showDialog = false
    }
  }

}
</script>
